package com.fgxia.user.controller;

import com.fgxia.user.domain.bo.UserOrderInfoBo;
import com.fgxia.user.domain.vo.DateNodeVo;
import com.fgxia.user.domain.vo.FrightDetailVo;
import com.fgxia.user.service.IOrderInfoService;
import com.ruoyi.business.domain.vo.OrderInfoVo;
import com.ruoyi.business.service.ISysConfigService;
import com.ruoyi.business.utils.OrderUtils;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.OrderInfoConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.OrderStatus;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单信息
 *
 * @author yyt
 * @date 2024-04-20
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/user/order")
public class OrderInfoController extends BaseController {

    private final IOrderInfoService orderInfoService;

    private final ISysConfigService sysConfigService;

    /**
     * 新增订单信息
     *
     * @param orderInfoBo
     * @return
     */
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@RequestBody UserOrderInfoBo orderInfoBo) {
        orderInfoBo.setRelevanceUserId(getUserId());
        orderInfoBo.setOrderNo(OrderUtils.generateOrderNo(getUserId()));
        orderInfoBo.setOrderStatus(OrderStatus.INITIALIZED.getCode());
        Boolean result = orderInfoService.insertByBo(orderInfoBo);
        return toAjax(result);
    }

    /**
     * 查询订单信息列表
     *
     * @param pageQuery
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo<OrderInfoVo> list(@RequestParam List<String> orderStatus, PageQuery pageQuery) {
        // 限制了只能查询自己的订单
        return orderInfoService.selectVoList(getUserId(), orderStatus, pageQuery);
    }


    /**
     * 生成时间段
     *
     * @return
     */
    @GetMapping("/time-slot")
    public R<List<DateNodeVo>> generateTimeSlot() {
        String configValue = sysConfigService.selectConfigByKey("order.timeSlot.daysAhead");
        int daysAhead = StringUtils.isNotEmpty(configValue) ?
            Integer.valueOf(configValue) : OrderInfoConstants.DEFAULT_DAYS_AHEAD;
        return R.ok(orderInfoService.generateTimeSlot(daysAhead));
    }

    @GetMapping("/detail/{orderId}")
    public R<List<FrightDetailVo>> detail(@PathVariable Long orderId) {
        return R.ok(orderInfoService.selectDetailById(getUserId(), orderId));
    }
}
